// Copyright 2007 Google Inc.  All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.enterprise.onebox.apps;

import junit.framework.TestCase;

import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.google.gdata.client.Query;
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.BaseFeed;
import com.google.gdata.data.docs.DocumentListFeed;


/**
 * This class is a test case that verifies several properties of the DocsProvider
 * implementation, namely:
 * (1) The provider returns null when authentication fails.
 * (2) The provider returns null when query fails.
 * (3) The provider actually performs the right query and returns some results.
 * 
 * @author amsmith@google.com (Adam Smith)
 */
public class DocsProviderTest extends TestCase {

  private DocsProvider provider;
  private DocsService riggedService;
  
  @Override
  public void setUp() throws Exception {
    super.setUp();
    provider = new DocsProvider();
    riggedService = new RiggedDocsService("whatever");
  }
  
  public void testHandlesBadAuthentication() {
    assertNull(provider.getResults("badQuery", "badUser", "badPass",
        riggedService));
  }
  
  public void testHandlesBadQuery() {
    assertNull(provider.getResults("badQuery", "goodUser", "goodPass",
        riggedService));
  }
  
  public void testReturnsSomeResults() {
    assertNotNull(provider.getResults("goodQuery", "goodUser", "goodPass",
        riggedService));
  }
  
  public static class RiggedDocsService extends DocsService {
    public RiggedDocsService(String serviceName) {
      super(serviceName);
    }
    
    public static final String U = "goodUser";
    public static final String P = "goodPass";
    public static final String Q = "goodQuery";
    
    @Override
    public void setUserCredentials(String user, String password) 
    throws AuthenticationException {
      if (!user.equals(U) || !password.equals(P)) {
        throw new AuthenticationException("should be " + U + "/" + P);
      }
    }
    
    @Override
    public BaseFeed query(Query query, Class feedClass)
    throws ServiceException {
      if (!query.getQueryUri().getQuery().substring(2).startsWith(Q)) {
        throw new ServiceException("test query should be " + Q);
      }
      return new DocumentListFeed();
    }
  }
  
}
